const app = getApp(); //引入导航

var util = require('../../utils/common.js'); //引入公共JS
var wxParse = require('../../wxParse/wxParse.js'); //引入
import { ycSelect } from 'ycSelect.model.js';
var ycselect = new ycSelect();
var performId = '';
var price = ''; //演出消息

var priceid = ''; //演出价格id

var z = 'DY';
var num2 = '';
Page({
  data: {
    showIcon: true,
    //显示返回键以及首页
    stockFlag: true,
    //缺货登记
    show: false,
    //控制下拉列表的显示隐藏，false隐藏、true显示
    selectData: ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
    //下拉列表的数据
    index: 0,
    //选择的下拉列表下标
    phone: '',
    //手机号
    number: '',
    //数量
    showMessage: false,
    //点击弹出的缺货登记提示显示
    messageContent: '',
    //点击弹出的缺货登记提示内容
    submitMessage: false,
    //点击弹出的缺货登记提示显示
    time: "",
    //默认选中第一个时间
    price: "",
    //默认选中第一个不是缺货的时间
    time_num: "",
    //默认选中第一个时间下标
    price_num: "",
    //默认选中第一个不是缺货的时间下标
    select_total: 0,
    //总票价
    num: 1,
    //默认选择的票价数量
    minusStatus: 'disabled',
    //数量小于1禁止点击状态
    noLogin: false,
    //未登录
    limit: 10 //数量限制

  },
  onLoad: function (options) {
    console.log(options)
    z = options.z;
    price = options;
    tt.setStorageSync('aid',options.aid);
    this.setData({
      aid: options.aid,
      title: options.title
    });

    this._loadDate(); //页面打开判断是否登陆过

  },
  _loadDate: function () {
    //特殊说明
    ycselect.getTebie(this.data.aid, data => {
      wxParse.wxParse('article', 'html', data.tebieshuoming, this, 5);
      this.setData({
        'shuoming': data.tebieshuoming
      });
    });
    var that = this;
    ycselect.getFare(this.data.aid, rowdata => {
      console.log(rowdata);
      tt.setStorageSync('performId', rowdata[0].id);
      var firstTime = rowdata[0].datetime;
      var firstID = rowdata[0].id;
      that.setData({
        listFare: rowdata,
        time_num: firstID,
        time: firstTime
      });
      ycselect.getOrder(rowdata[0].aid, rowdata[0].id, data => {
        console.log(data);
        that.setData({
          'listOrder': data
        });
      });
    });
  },
  queHuo: function (e) {
    console.log(e);
    var data = e.detail.value; //表单消息
    var aid = price['aid']; //演出aid
    var title = price['title'];
    var phone = data['phone']; //用户手机号
    var quehuo_num = data['quehuo_num']; //缺货数量
    var word = data['word']; //留言
    var telRule = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[1|3|5|8|9])[0-9]{8}$/;

    if (phone == '') {
      this.showMessage('手机号不能为空');
    } else if (!telRule.test(phone)) {
      this.showMessage('手机号格式不正确');
    } else {
      ycselect.getquehuo(aid, phone, quehuo_num, word, priceid, res => {
        console.log(res);
        this.setData({
          stockFlag: "true"
        });
      });
      this.showMessage('提交成功');
    }
  },
  selectTap: function (e) {
    // 点击下拉显示框
    this.setData({
      show: !this.data.show
    });
  },
  optionTap: function (e) {
    // 点击下拉列表
    var Index = e.currentTarget.dataset.index; //获取点击的下拉列表的下标

    this.setData({
      index: Index,
      show: !this.data.show
    });
  },
  phoneInput: function (e) {
    //监听手机号输入
    this.data.phone = e.detail.value;
  },
  wordInput: function (e) {
    //监听留言输入
    this.data.word = e.detail.value;
  },
  stockRefer: function () {
    var _index = this.data.index; //点击数量的下标
    var _phone = this.data.phone; //手机号
    var _num = this.data.selectData[_index]; //下拉选择的数量
    var _word = this.data.word; //留言
    var telRule = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[1|3|5|8|9])[0-9]{8}$/; //验证手机号
    if (_phone == '') {
      this.showMessage('手机号不能为空');
    } else if (!telRule.test(_phone)) {
      this.showMessage('手机号格式不正确');
    }
  },
  showMessage: function (text) {
    //打开关闭提示弹框
    var that = this;
    that.setData({
      showMessage: true,
      messageContent: text
    });
    setTimeout(function () {
      that.setData({
        showMessage: false,
        messageContent: ''
      });
    }, 3000);
  },
  selectTime: function (e) {
    //点击选择时间
    var _time = e.currentTarget.dataset.time; //点击选择获取时间
    var _num = e.currentTarget.dataset.num; //点击选择获取时间的下标
    var _id = e.target.id;

    if (_id == "disabled") {
      // 判断当缺货时打开缺货登记
      this.setData({
        stockFlag: false
      });
    } else {
      // 当点击非缺货时
      this.setData({
        time_num: _num,
        time: _time,
        'select_total': 0,
        price_num: "",
        num: 1,
        price: 0
      });
    }

    var aid = e.target.dataset.aid;
    performId = e.target.dataset.num;
    tt.setStorageSync('aid', aid);
    tt.setStorageSync('performId', performId); //时间id

    this.data.aid = aid;
    this.data.performId = performId;
    console.log(_time);
    ycselect.getOrder(this.data.aid, this.data.performId, data => {
      //console.log(data)
      this.setData({
        'listOrder': data
      });
    });
  },
  selectPrice: function (e) {
    //选择票价
    console.log(e);
    var count = e.currentTarget.dataset.count; //套票属性
    if (count) {} else {
      count = 1;
    }

    var _price = e.currentTarget.dataset.price; //点击选择获取价格      真实价格
    var pmprice = e.currentTarget.dataset.pmprice; //点击选择获取价格  票面价格
    var _num = e.currentTarget.dataset.num; //点击选择获取价格的下标
    var _limit = e.currentTarget.dataset.limit; //点击选择购票数量

    priceid = _num;
    var pricetitle = e.currentTarget.dataset.pricetitle;
    var totalPrice = 1 * _price;
    var _id = e.target.id;
    var stock = e.currentTarget.dataset.stock; //console.log(pricetitle)

    tt.setStorageSync('pmprice', pmprice);
    tt.setStorageSync('stock', stock);
    tt.setStorageSync('price', _price);
    tt.setStorageSync('quyuID', _num);
    tt.setStorageSync('priceID', _num);
    tt.setStorageSync('pricetitle', pricetitle);
    tt.setStorageSync('count', count);

    if (_id == "disabled") {
      // 判断当缺货时打开缺货登记
      this.setData({
        stockFlag: false
      });
    } else {
      // 当点击非缺货时
      this.setData({
        price_num: _num,
        price: _price,
        select_total: totalPrice,
        limit: _limit,
        num: 1 //重置数量

      });
    }
  },
  stockHide: function (e) {
    // 关闭缺货登记
    this.setData({
      stockFlag: true
    });
  },

  /* 点击减号 */
  bindMinus: function () {
    var num = this.data.num; //选择的数量
    var price = this.data.price; //选中的票价
    // 如果大于1时，才可以减

    if (num > 1) {
      num--;
    } // 只有大于一件的时候，才能normal状态，否则disable状态


    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    var totalPrice = num * price; // 将数值与状态写回

    this.setData({
      num: num,
      minusStatus: minusStatus,
      select_total: totalPrice
    });
  },

  /* 点击加号 */
  bindPlus: function () {
    var num = this.data.num;
    var price = this.data.price; //选中的票价
    var limit = this.data.limit; // 数量最多可以选择10件

    if (num < limit) {
      num++;
    }

    if (num == limit) {
      tt.showToast({
        title: '此票最多购买' + limit + '张',
        icon: 'none',
        duration: 1000
      });
    } // 只有大于一件的时候，才能normal状态，否则disable状态


    var minusStatus = num < 1 ? 'disabled' : 'normal';
    var totalPrice = num * price; // 将数值与状态写回

    this.setData({
      num: num,
      minusStatus: minusStatus,
      select_total: totalPrice
    });
  },
  submitClick: function (e) {
    console.log(111, e);
    var that = this;
    var num = this.data.num; //选择的数量
    var stock = tt.getStorageSync("stock");

    if (num > stock) {
      tt.showToast({
        title: '库存不足',
        icon: 'none',
        duration: 3000
      });
    } else {
      var price = this.data.price; //选中的票价
      var time = this.data.time; //选中的时间
      var orderStatus = 0;
      var aid = tt.getStorageSync("aid");
      var performId = tt.getStorageSync("performId");
      var quyuID = tt.getStorageSync("quyuID");

      if (price == '' || time == '') {
        that.setData({
          submitMessage: true
        });
        setTimeout(function () {
          that.setData({
            submitMessage: false
          });
        }, 3000);
      } else {
        tt.navigateTo({
          url: '../SubmitYcOrder/SubmitYcOrder?aid=' + aid + '&num=' + num + '&performId=' + performId + '&quyuID=' + quyuID + '&orderstatus=' + orderStatus + '&z=' + z
        });
      }
    }
  },
  //分享
  onShareAppMessage: function () {
    util.sharePage();
  }
});